package GUI;

import connect.ConnectDB;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class JobRole_ChooseEmp extends javax.swing.JDialog {

    ConnectDB connect;
    ResultSet rs = null;
    String EmpId = "";

    public JobRole_ChooseEmp(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        connect = new ConnectDB();
        connect.getConnect();
        initComponents();
        loadTableAll();
        if (rdbNotConfirm.isSelected()) {
            loadTableNotConfirm();
        }

        tblJob.setSelectionMode(0);
        btnChoose.setEnabled(false);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jRadioButton2 = new javax.swing.JRadioButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblJob = new javax.swing.JTable(){
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        jLabel1 = new javax.swing.JLabel();
        rdbAll = new javax.swing.JRadioButton();
        rdbNotConfirm = new javax.swing.JRadioButton();
        rdbConfirm = new javax.swing.JRadioButton();
        rdbDeny = new javax.swing.JRadioButton();
        btnCancel = new javax.swing.JButton();
        btnChoose = new javax.swing.JButton();

        jRadioButton2.setText("jRadioButton2");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("JOB ROTATIO INFORMATION");

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        jLabel2.setBackground(new java.awt.Color(0, 0, 153));
        jLabel2.setFont(new java.awt.Font("Segoe UI Semilight", 0, 24)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("CHOOSE EMPLOYEE");
        jLabel2.setOpaque(true);

        tblJob.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblJob.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tblJobMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(tblJob);

        jLabel1.setFont(new java.awt.Font("Segoe UI Light", 1, 18)); // NOI18N
        jLabel1.setText("Status :");

        rdbAll.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroup1.add(rdbAll);
        rdbAll.setFont(new java.awt.Font("Segoe UI", 0, 13)); // NOI18N
        rdbAll.setText("All");
        rdbAll.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                rdbAllItemStateChanged(evt);
            }
        });

        rdbNotConfirm.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroup1.add(rdbNotConfirm);
        rdbNotConfirm.setFont(new java.awt.Font("Segoe UI", 0, 13)); // NOI18N
        rdbNotConfirm.setSelected(true);
        rdbNotConfirm.setText("Not Comfirmed");
        rdbNotConfirm.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                rdbNotConfirmItemStateChanged(evt);
            }
        });

        rdbConfirm.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroup1.add(rdbConfirm);
        rdbConfirm.setFont(new java.awt.Font("Segoe UI", 0, 13)); // NOI18N
        rdbConfirm.setText("Confirmed");
        rdbConfirm.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                rdbConfirmItemStateChanged(evt);
            }
        });

        rdbDeny.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroup1.add(rdbDeny);
        rdbDeny.setFont(new java.awt.Font("Segoe UI", 0, 13)); // NOI18N
        rdbDeny.setText("Deny");
        rdbDeny.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                rdbDenyItemStateChanged(evt);
            }
        });

        btnCancel.setBackground(new java.awt.Color(255, 255, 255));
        btnCancel.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnChoose.setBackground(new java.awt.Color(255, 255, 255));
        btnChoose.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
        btnChoose.setText("Choose");
        btnChoose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChooseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 673, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnChoose, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(btnCancel)))
                .addContainerGap())
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(126, 126, 126)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(rdbNotConfirm)
                .addGap(18, 18, 18)
                .addComponent(rdbConfirm)
                .addGap(18, 18, 18)
                .addComponent(rdbDeny)
                .addGap(18, 18, 18)
                .addComponent(rdbAll)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnCancel, btnChoose});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(rdbAll)
                    .addComponent(rdbDeny)
                    .addComponent(rdbConfirm)
                    .addComponent(rdbNotConfirm)
                    .addComponent(jLabel1))
                .addGap(24, 24, 24)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnChoose, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnCancel, btnChoose});

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 11, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void rdbNotConfirmItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_rdbNotConfirmItemStateChanged
        if (rdbNotConfirm.isSelected()) {
            loadTableNotConfirm();
            btnChoose.setEnabled(false);
        }
    }//GEN-LAST:event_rdbNotConfirmItemStateChanged

    private void btnChooseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChooseActionPerformed
        int n = tblJob.getSelectedRow();
        EmpId = tblJob.getValueAt(n, 0).toString();
        connect.closeConnect();
        
        this.dispose();

    }//GEN-LAST:event_btnChooseActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        connect.closeConnect();
        this.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void rdbConfirmItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_rdbConfirmItemStateChanged
        if (rdbConfirm.isSelected()) {
            loadTableConfirm();
            btnChoose.setEnabled(false);
        }
    }//GEN-LAST:event_rdbConfirmItemStateChanged

    private void rdbDenyItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_rdbDenyItemStateChanged
        if (rdbDeny.isSelected()) {
            loadTableDeny();
            btnChoose.setEnabled(false);
        }
    }//GEN-LAST:event_rdbDenyItemStateChanged

    private void rdbAllItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_rdbAllItemStateChanged
        if (rdbAll.isSelected()) {
            loadTableAll();
            btnChoose.setEnabled(false);
        }
    }//GEN-LAST:event_rdbAllItemStateChanged

    private void tblJobMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblJobMouseReleased
        int n = tblJob.getSelectedRow();
        if (evt.getClickCount() == 1) {
            if (tblJob.getValueAt(n, 3).toString().equals("Not Confirmed")) {
                btnChoose.setEnabled(true);
                return;
            }
            btnChoose.setEnabled(false);
        }
        
        if (evt.getClickCount() == 2) {
            if (n != -1) {
                if (tblJob.getValueAt(n, 3).toString().equals("Not Confirmed")) {
                    EmpId = tblJob.getValueAt(n, 0).toString();
                    connect.closeConnect();
                    this.dispose();
                }

            }
        }
    }//GEN-LAST:event_tblJobMouseReleased

    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JobRole_ChooseEmp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JobRole_ChooseEmp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JobRole_ChooseEmp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JobRole_ChooseEmp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JobRole_ChooseEmp dialog = new JobRole_ChooseEmp(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnChoose;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton rdbAll;
    private javax.swing.JRadioButton rdbConfirm;
    private javax.swing.JRadioButton rdbDeny;
    private javax.swing.JRadioButton rdbNotConfirm;
    private javax.swing.JTable tblJob;
    // End of variables declaration//GEN-END:variables

    public void loadTableAll() {
        try {
            String query = "SELECT "
                    + "             * "
                    + "     FROM "
                    + "             JobRotation ";
            Statement st = connect.conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rs = st.executeQuery(query);

            Vector colName = new Vector();
            colName.add("Employee ID");
            colName.add("Present Design");
            colName.add("Depute to");
            colName.add("Status");

            DefaultTableModel dtm = new DefaultTableModel(colName, 0);

            while (rs.next()) {
                Vector rowData = new Vector();
                rowData.add(rs.getString("EMPID"));
                rowData.add(rs.getString("PRESENTDESIGNATION"));
                rowData.add(rs.getString("DEPUTEDTO"));
                if (rs.getString("STATUS").equals("0")) {
                    rowData.add("Not Confirmed");
                } else if (rs.getString("STATUS").equals("1")) {
                    rowData.add("Confirmed");
                } else {
                    rowData.add("Deny");
                }

                dtm.addRow(rowData);
            }
            tblJob.setModel(dtm);
        } catch (Exception e) {
            System.out.println("Error in Vac_Fill_Details_Table : " + e.getMessage());
        }
    }

    public void loadTableNotConfirm() {
        try {
            Vector colName = new Vector();
            colName.add("Employee ID");
            colName.add("Present Design");
            colName.add("Depute to");
            colName.add("Status");

            DefaultTableModel dtm = new DefaultTableModel(colName, 0);

            rs.beforeFirst();
            while (rs.next()) {
                if (rs.getString("STATUS").equals("0")) {
                    Vector rowData = new Vector();
                    rowData.add(rs.getString("EMPID"));
                    rowData.add(rs.getString("PRESENTDESIGNATION"));
                    rowData.add(rs.getString("DEPUTEDTO"));
                    rowData.add("Not Confirmed");
                    dtm.addRow(rowData);
                }
            }
            tblJob.setModel(dtm);
        } catch (Exception e) {
            System.out.println("Error in loadTableNotConfirm: " + e.getMessage());
        }
    }
    
    public void loadTableDeny(){
        try {
            Vector colName = new Vector();
            colName.add("Employee ID");
            colName.add("Present Design");
            colName.add("Depute to");
            colName.add("Status");

            DefaultTableModel dtm = new DefaultTableModel(colName, 0);

            rs.beforeFirst();
            while (rs.next()) {
                if (rs.getString("STATUS").equals("2")) {
                    Vector rowData = new Vector();
                    rowData.add(rs.getString("EMPID"));
                    rowData.add(rs.getString("PRESENTDESIGNATION"));
                    rowData.add(rs.getString("DEPUTEDTO"));
                    rowData.add("Deny");
                    dtm.addRow(rowData);
                }
            }
            tblJob.setModel(dtm);
        } catch (Exception e) {
            System.out.println("Error in loadTableNotConfirm: " + e.getMessage());
        }
    }
    
    public void loadTableConfirm(){
        try {
            Vector colName = new Vector();
            colName.add("Employee ID");
            colName.add("Present Design");
            colName.add("Depute to");
            colName.add("Status");

            DefaultTableModel dtm = new DefaultTableModel(colName, 0);

            rs.beforeFirst();
            while (rs.next()) {
                if (rs.getString("STATUS").equals("1")) {
                    Vector rowData = new Vector();
                    rowData.add(rs.getString("EMPID"));
                    rowData.add(rs.getString("PRESENTDESIGNATION"));
                    rowData.add(rs.getString("DEPUTEDTO"));
                    rowData.add("Confirmed");
                    dtm.addRow(rowData);
                }
            }
            tblJob.setModel(dtm);
        } catch (Exception e) {
            System.out.println("Error in loadTableNotConfirm: " + e.getMessage());
        }
    }
    
    public String getEmpId() {
        return EmpId;
    }
}
